
/*\
|*|
|*|  :: XMLHttpRequest.prototype.sendAsBinary() Polyfill ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#sendAsBinary()
\*/

if (!XMLHttpRequest.prototype.sendAsBinary) {
    XMLHttpRequest.prototype.sendAsBinary = function(sData) {
        var nBytes = sData.length, ui8Data = new Uint8Array(nBytes);
        for (var nIdx = 0; nIdx < nBytes; nIdx++) {
            ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff;
        }
        /* send as ArrayBufferView...: */
        this.send(ui8Data);
        /* ...or as ArrayBuffer (legacy)...: this.send(ui8Data.buffer); */
    };
}

/**
 * 模拟表单上传，构造边界
 * @param oField
 */
function formatFileData(oField){
    for (nFile = 0; nFile < oField.files.length; nFile++) {
        oFile = oField.files[nFile];
        oSegmReq = new FileReader();
        /* (custom properties:) */
        oSegmReq.segmentIdx = this.segments.length;
        oSegmReq.owner = this;
        /* (end of custom properties) */
        oSegmReq.onload = pushSegment;
        this.segments.push("Content-Disposition: form-data; name=\"" +
            oField.name + "\"; filename=\"" + oFile.name +
            "\"\r\nContent-Type: " + oFile.type + "\r\n\r\n");
        this.status++;
        oSegmReq.readAsBinaryString(oFile);
    }
}
